#include "BlackDeath.Core.h"

namespace BlackDeath
{
	namespace Graphics
	{
		#pragma region Constructor and Destructor
		CIndexBuffer::CIndexBuffer(CGraphicsDevice* graphicsDevice, int length, int stride, D3D10_USAGE usage)
		{
			this->m_pGraphicsDevice = graphicsDevice;
			this->m_iLength = length;
			this->m_iStride = stride;
			this->m_eBufferUsage = usage;
			this->m_iBuffer = NULL;
		}

		CIndexBuffer::~CIndexBuffer()
		{
			if(this->m_iBuffer != NULL)
				this->m_iBuffer->Release();
		}
		#pragma endregion

		#pragma region Initialize
		void CIndexBuffer::CreateBuffer(void* data)
		{
			D3D10_BUFFER_DESC bd;
			bd.Usage = this->m_eBufferUsage;
			bd.ByteWidth = this->Get_SizeInBytes();
			bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
			bd.CPUAccessFlags = 0;
			bd.MiscFlags = 0;

			D3D10_SUBRESOURCE_DATA vbData;
			vbData.pSysMem = data;

			HRESULT hr = this->m_pGraphicsDevice->m_pGraphicsDevice->CreateBuffer(&bd, &vbData, &this->m_iBuffer);
			if(FAILED(hr))
				throw BlackDeath::Exception::CBlackDeathException::CBlackDeathException("Unable to create index buffer");
		}
		#pragma endregion

		#pragma region Methods
		void CIndexBuffer::SetData(void* buffer)
		{
			this->CreateBuffer(buffer);
		}

		int CIndexBuffer::Get_SizeInBytes()
		{
			return this->m_iLength * this->m_iStride;
		}

		int CIndexBuffer::Get_Length()
		{
			return this->m_iLength;
		}
		#pragma endregion
	}
}